package zuoye5;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class ClientTest {
    public static void main(String[] args) {
        DatagramPacket dp=null;
        DatagramSocket ds = null;
        Scanner sc=new Scanner(System.in);
        try {
            ds=new DatagramSocket();
            byte[]data=new byte[1024];
            while (true) {
                String mess=sc.nextLine();
                byte[] buf=mess.getBytes();
                dp=new DatagramPacket(buf,buf.length, InetAddress.getByName("localhost"),8989);
                ds.send(dp);


                dp=new DatagramPacket(data,data.length);
                ds.receive(dp);
                mess=new String(dp.getData(),0,dp.getLength());
                System.out.println("服务器响应为："+mess);
            }

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            ds.close();
        }
    }
}
